package com.act.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.act.constant.ResponseEnum;

/**
 * @author Javis
 * @@desc 响应实体
 */

@JsonInclude(JsonInclude.Include.NON_NULL)
public abstract class Response {

    /* 响应结果编号 */
    protected int resultCode;

    public int getResultCode() {
        return resultCode;
    }

    /**
     * @desc 响应成功
     * @return
     */
    public static ResponseSuccess success() {
        return (ResponseSuccess) getResponse(ResponseEnum.SUCCESS, null, null);
    }

    /**
     * @desc 响应成功并返回数据
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> ResponseSuccess success(T data) {
        return (ResponseSuccess) getResponse(ResponseEnum.SUCCESS, null, data);
    }

    /**
     *@desc  响应失败
     * @return
     */
    public static ResponseFail fail() {
        return (ResponseFail) getResponse(ResponseEnum.FAIL, null, null);
    }

    /**
     * @desc 响应失败并返回失败描述
     * @param description 描述
     * @return
     */
    public static ResponseFail fail(String description) {
        return (ResponseFail) getResponse(ResponseEnum.FAIL, description, null);
    }

    /**
     * @desc 用户未登录
     * @return
     */
    public static ResponseLoseSession loseSession() {
        return (ResponseLoseSession) getResponse(ResponseEnum.FAIL, null, null);
    }

    /**
     * @desc ResponseFactory
     * @param responseEnum
     * @param desc
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Response getResponse(ResponseEnum responseEnum, String desc, T data) {
        switch (responseEnum) {
            case SUCCESS:
                if (data != null) {
                    return new ResponseSuccess<T>(data);
                }
                return new ResponseSuccess<T>();
            case FAIL:
                return new ResponseFail();
            case NOT_LOGIN:
                return new ResponseLoseSession();
            default:
                return null;
        }
    }

}
